<?php

/**
 * manage blog with comment
 * @author Tuong Tran <tuong.tran@outlook.com>
 */
class BusStationController extends Controller {

    private $_model;
    public $activeMenu = 'bus';

    public function actionIndex($id) {
//        $models = BusStation::model()->findAll();
//        
//        foreach($models as $model) {
//            $detail = BusDetail::model()->find('bus_id = ' . $model->bus_id . ' AND direction = ' . $model->direction);
//            
//            $model->bus_detail_id = $detail->bus_detail_id;
//            
//            if(!$model->save()) {
//                echo 'not saved';
//            }
//        }
//        die();
        
        $bus = $this->loadModelBus();
        $models = new BusStation();
        
        $models->unsetAttributes();  // clear any default values
        if (isset($_GET['BusStation']))
            $models->attributes = $_GET['BusStation'];

        $this->render('index', array(
            'models' => $models,
            'bus' => $bus,
        ));
    }

    public function actionCreate($id) {
        $model = new BusStation();
        
        $bus = $this->loadModelBus();
        
        if (isset($_POST['BusStation'])) {
            $model->attributes = $_POST['BusStation'];

            if ($bus->saveStation($model)) {
                Yii::app()->user->setFlash('success', 'BusStation successfully added');
                $this->redirect('index', array('id' => $bus->bus_id));
            }
        }

        $this->render('create', compact('model', 'bus'));
    }

    public function actionUpdate($id) {
        //$model = $this->loadModel();

        $model = $this->loadModel();
        
        if (isset($_POST['BusStation'])) {
            $model->attributes = $_POST['BusStation'];

            if ($model->save()) {
                Yii::app()->user->setFlash('success', 'BusStation successfully updated');
                $this->redirect('index');
            }
        }

        $this->render('create', compact('model'));
    }

    public function actionDelete($id) {

        if (Yii::app()->request->isPostRequest) {
            // we only allow deletion via POST request

            $this->loadModel()->delete();

            // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser

            if (!isset($_GET['ajax']))
                $this->redirect(array('index'));
        }

        else
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
    }
    

    public function loadModel() {

        if ($this->_model === null) {

            if (isset($_GET['id'])) {

                $this->_model = BusStation::model()->findByPk($_GET['id']);
            }

            if ($this->_model === null)
                throw new CHttpException(404, 'The requested page does not exist.');
        }

        return $this->_model;
    }
    
    public function loadModelBus($id = NULL) {
        
        if($id === NULL) {
            $id = $_GET['id'];
        }

        if ($this->_model === null) {

            if (isset($id)) {

                $this->_model = Bus::model()->findByPk($id);
            }

            if ($this->_model === null)
                throw new CHttpException(404, 'The requested page does not exist.');
        }

        return $this->_model;
    }

}